package boogui.admin;

import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;

import boogui.movie.MovieVO;

public class ShowingRegistryService implements IShowingRegistryService {
	
	private ShowingRegistryDAO srd = null;
	private MovieRegistryService mrs = null;

	public ShowingRegistryService() {		
		srd = new ShowingRegistryDAO();
		mrs = new MovieRegistryService();
	}	
	
	// 지역 콤보박스
	@Override
	public void locationCombo(JComboBox<String> c) {
		for (MovieVO mv : srd.allLocation()) {
			if (c!=null)c.addItem(mv.getLocation());
		}
	}

	// 영화관 콤보박스
	@Override
	public void cinemaCombo(JComboBox<String> c, String list) {
		/*DefaultComboBoxModel<String> loCbx = new DefaultComboBoxModel<>();		
		for (MovieAreaVO mv : md.locationCinema(list)) {
			loCbx.addElement(mv.getCinename());
		}
		if (c == null) System.out.println("널이얌");
		else c.setModel(loCbx);*/
		
		if (c != null)c.removeAllItems();
		for (MovieVO mv : srd.locationCinema(list)) {				
			if (c != null)c.addItem(mv.getCinename());
		}
	}

	// 상영관 콤보박스
	@Override
	public void theaterCombo(JComboBox<String> c, String list) {		
		if (c != null)c.removeAllItems();
		for (MovieVO mv : srd.cinemaTheater(list)) {				
			if (c != null)c.addItem(mv.getTheater());
		}
	}
	
	// 영화 제목 조회 (전체)
	@Override
	public void readMovieTitle(JComboBox<String> c) {
		for (String s : srd.selectMovieTitle()){
			c.addItem(s);
		}
	}

	// 상영정보 등록
	@Override
	public boolean creatShowing(MovieVO m) {
		boolean result = false;
		if (srd.insertShowing(m)) {
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	// 테이블에서 값 받아와서 상영정보 등록
	@Override
	public boolean getTableModel(DefaultTableModel model) {
		String str = "";		
		boolean result = false;
		boolean result1 = false;
		for (int i = 0; i < model.getRowCount(); ++i) {
			MovieVO m = new MovieVO();
			m.setMovtitle((String) model.getValueAt(i, 0));
			m.setLocation((String) model.getValueAt(i, 1));
			m.setCinename((String) model.getValueAt(i, 2));
			m.setTheater((String) model.getValueAt(i, 3));
			str = mrs.extractNum((String) model.getValueAt(i, 4));
			m.setSdate(str);
			m.setStime((String) model.getValueAt(i, 5));
			result = this.creatShowing(m);
			result1 = this.createSeat();
		}		
		return result&&result1;
	}
	
	// 좌석 정보 등록
	public boolean createSeat() {
		String sql = "insert into seatinfo values (" + srd.selectShowNo() + "";
		for (int i = 0; i < 180; i++) {
			sql += ",'O'";
		}
		sql += ")";
		return srd.insertSeat(sql);
	}
}
